package classReflectDemo;

import org.junit.Test;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 入口
 * 
 * @author jie
 *
 */
public class Reflect_Index {


	//测试 父类 拿到子类东西
	@Test
	public void mainLift2Person() {
		try {
			// 1
			Class clazz = Person.class;

			Life life = new Person();//与 entity.Person.class;结果相同

			System.out.println("clazz: "+life.getClass());
			// 2得到方法
			Method method = clazz.getDeclaredMethod("getName", String.class);
			Method me_getSex = clazz.getDeclaredMethod("getSex", null);

			// 3调用
			// 静态方法 不用穿对象 直接写null
			Method getFileMethod = life.getClass().getDeclaredMethod("getFile",null);
			getFileMethod.setAccessible(true);
			File invoke1 = (File) getFileMethod.invoke(null, null);
			System.out.println("getFileMethod: "+invoke1.getName());


			Object invoke = method.invoke(null, "abc");
			System.out.println("static method: "+invoke.toString());

			Object invoke2 = me_getSex.invoke(life, null);
			System.out.println("method: "+invoke2);

		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


}

class Life {
	int legs;
	int hands;

	public int getLegs() {
		return legs;
	}

	public void setLegs(int legs) {
		this.legs = legs;
	}

	public int getHands() {
		return hands;
	}

	public void setHands(int hands) {
		this.hands = hands;
	}
}

// entity.Person 是谁的对象？Class
// 都有 类名 属性 方法 构造
class Person extends Life{

	String name;
	static int age;
	private String sex;

	public static String getName(String str) {
		return "张三";
	}

	public String getSex() {
		return sex + "abc";
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	private static File getFile(){
		return  new File("abc");
	}


}

class Dog {

	String name;
	static int age;
}

// 假设的Class 类 应该有的 共同的属性
// java.long.class
class MyClass {
	// String className;
	// PackageName;
	// fields;
	// methods;
}
